﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Net;

using XMK.Tadong.Model;
using XMK.Tadong.Biz;
using XMK.Utils;
using XMK.Framework.Model;
using XMK.Tadong.Model.Cond;
using XMK.Tadong.Web.Language;
using System.Configuration;

namespace XMK.Tadong.Web.Views.DataSearch
{
    public partial class WatchUnkownView : FileExplorerPage
    {
        public string PageNum;
        public string swfpath;
        public string unkownFilePrePicDisk;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string spaceType = Request.Params.Get("space_type");
                this.Image1.ImageUrl ="../../WebFileExplorer/images/ico/unkownFile.jpg";
                unkownFilePrePicDisk = ConfigurationManager.AppSettings["unkownFilePrePicDisk"];
                if (string.IsNullOrWhiteSpace(spaceType)) h_spaceType.Value = "30";
                else
                    h_spaceType.Value = spaceType;


                if (XMKFileExtInfoModel == null
                || string.IsNullOrWhiteSpace(XMKFileExtInfoModel.FILE_NAME.Trim()))
                {
                    _xmkFileExtInfoModel = new XMKFileExtInfo
                    {
                        FILE_NAME = string.Empty,
                        SwfFilePath = string.Empty
                    };
                    ClientScript.RegisterStartupScript(this.GetType(),
                        "alertclose", "<script> alert(" + lang.Get("Time consuming") + "); window.close();</script>");
                }
                else
                {
                    WriteLog(LogOperateType.ScanFile, XMKFileExtInfoModel.FILE_NAME);
                }
            }

        }

        private XMKFileExtInfo _xmkFileExtInfoModel;
        public XMKFileExtInfo XMKFileExtInfoModel
        {
            get
            {
                string DiskPath = ConfigurationManager.AppSettings["DiskPath"];
                if (_xmkFileExtInfoModel == null)
                {
                    _xmkFileExtInfoModel = new XMKFileExtInfo
                    {
                        FILE_NAME = String.Empty,
                        SwfFilePath = string.Empty
                    };

                    string fileID = Request.QueryString["id"];
                    if (!string.IsNullOrWhiteSpace(fileID))
                    {
                        if (h_spaceType.Value.Equals("30"))
                        {
                            try
                            {
                                Tb_Xmk_FileBiz biz = new Tb_Xmk_FileBiz();
                                _xmkFileExtInfoModel = biz.GetXMKFileExtInfoByID(fileID);
                                if (_xmkFileExtInfoModel.PRE_PIC != "")
                                {
                                    this.Image1.ImageUrl = unkownFilePrePicDisk + _xmkFileExtInfoModel.PRE_PIC;
                                }
                            }
                            catch (Exception e)
                            {
                                ClientScript.RegisterStartupScript(this.GetType(),
                               "alertclose", "<script> alert(" + lang.Get("File converting") + "); window.close();</script>");
                            }
                        }
                        else if (h_spaceType.Value.Equals("20"))
                        {
                            
                            try
                            {
                                TB_TEAM_FILEBiz teamBiz = new TB_TEAM_FILEBiz();
                                _xmkFileExtInfoModel = teamBiz.GetModelByFileId(fileID);
                                if (_xmkFileExtInfoModel.PRE_PIC != "")
                                {
                                    this.Image1.ImageUrl = unkownFilePrePicDisk + _xmkFileExtInfoModel.PRE_PIC;
                                }
                            }
                            catch (Exception e)
                            {
                                ClientScript.RegisterStartupScript(this.GetType(),
                               "alertclose", "<script> alert(" + lang.Get("File converting") + "); window.close();</script>");
                            }
                        }

                        else if (h_spaceType.Value.Equals("10"))
                        {

                            try
                            {
                                TB_PERSON_FILEBiz perBiz = new TB_PERSON_FILEBiz();
                                _xmkFileExtInfoModel = perBiz.GetModelByFileId(fileID);
                                if (_xmkFileExtInfoModel.PRE_PIC != "")
                                {
                                    this.Image1.ImageUrl = unkownFilePrePicDisk + _xmkFileExtInfoModel.PRE_PIC;
                                }
                            }
                            catch (Exception e)
                            {
                                ClientScript.RegisterStartupScript(this.GetType(),
                               "alertclose", "<script> alert(" + lang.Get("File converting") + "); window.close();</script>");
                            }
                        }
                        if (_xmkFileExtInfoModel != null)
                        {
                            
                            if (!string.IsNullOrWhiteSpace(_xmkFileExtInfoModel.SAVE_PATH))
                            {
                                _xmkFileExtInfoModel.SAVE_PATH = Path.Combine(Config.DiskFilePath, _xmkFileExtInfoModel.SAVE_PATH).Replace("\\", "/");
                            }
                            string fileExt = Path.GetExtension(_xmkFileExtInfoModel.SAVE_PATH);

                            EXTBiz biz = new EXTBiz();
                            ExtTypeinfo extcode = biz.getExtCodeByExtent(fileExt.Replace(".", ""));
                            string path = "";
                            if (extcode != null)
                            {
                                ImgFileIcon.Src = string.Format("../../WebFileExplorer/images/ico/exttype/" + extcode.PIC_NAME, extcode.EXT_CODE);
                            }
                            else
                            {
                                ImgFileIcon.Src = string.Format("../../WebFileExplorer/images/ico/exttype/130901108129_2486.png", "unkown");
                            }

                        }
                    }
                }
                return _xmkFileExtInfoModel;
            }
        }


        protected bool CheckUserAuthority(int rightLevel)
        {

            Base_FileRoleOper fileRoleOper = new Base_FileRoleOper();
            if (h_spaceType.Value.ToString() == "30")
            {
                //下载源文件
                fileRoleOper.FileId = Request.QueryString["id"];//"4209";
                fileRoleOper.SpaceType = "30";
                fileRoleOper.OpreType = "30";
                
            }
            else if (h_spaceType.Value.ToString() == "20")
            {
                //下载源文件
                    fileRoleOper.FileId = Request.QueryString["id"];//"4209";
                    fileRoleOper.SpaceType = "20";
                    fileRoleOper.OpreType = "30";
               
            }
            else if (h_spaceType.Value.ToString() == "10")
            {
                return true;
            }
            Base_DictBiz dictBiz = new Base_DictBiz();
            ResponseInfo resp = dictBiz.GetUserFileRoleRight(fileRoleOper);
            if ((bool)resp.Data == true)
                return true;
            return false;
        }

        protected void btnDownloadSourceFile_Click(object sender, EventArgs e)
        {
            if (CheckUserAuthority(3))
            {
                FileDownload(XMKFileExtInfoModel.SAVE_PATH, XMKFileExtInfoModel.FILE_NAME);
                WriteLog(LogOperateType.DownFile, XMKFileExtInfoModel.FILE_NAME);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(),
                    "alertclose", "<script> alert(" + lang.Get("Insufficient_permissions_contact_your_administrator") + "); </script>");
            }
        }

        private void FileDownload(string filePath, string File_Name)
        {
            byte[] bytes;
            //以字符流的形式下载文件
            string fileName = Path.GetFileName(filePath);
            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            {

                bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
            }

            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(File_Name, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

        protected void btnAddPic_Click(object sender, EventArgs e)
        {
            string spaceType = Request.Params.Get("space_type");
            string fileID = Request.QueryString["id"];
            Response.Write("<script>window.showModelessDialog('../../webhtml/uploadPrePic.htm?fileid=" + fileID + "&space=" + spaceType + "','a','center:no;dialogLeft:100px;dialogTop:100px;scroll:0;status:0;help:0;resizable:0;dialogWidth:450px;dialogHeight:200px')</script>");
           
        }

        private void WriteLog(LogOperateType operateType, string fileName)
        {
            long fileid = XMKFileExtInfoModel.FILE_ID;
            string ip = Request.ServerVariables["REMOTE_ADDR"].Equals("::1") ? "127.0.0.1" : Request.ServerVariables["REMOTE_ADDR"];
            XMKLogBiz.WriteLogFile(operateType, fileName, fileid, h_spaceType.Value, ip);
        }
    }
}